package com.wiscamp.ninechapters.tags.infrastructure.mappers;

import com.wiscamp.ninechapters.tags.infrastructure.po.EntityView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;

/**
 * 应用实体仓储对象
 *
 * @author yifeng
 */
@Repository
public interface EntityViewMapper extends JpaRepository<EntityView, Long>, JpaSpecificationExecutor<EntityView> {
    /**
     * 通过应用编号和代码获取应用实体列表
     *
     * @param entityCode
     * @param applicationId
     * @return
     */
    @Query(value = "select e from EntityView e where e.applicationId = :applicationId and  e.entityCode = :entityCode and e.deleted = false")
    EntityView findByEntityCode(String entityCode, long applicationId);

    /**
     * 通过应用编号和名称获取应用实体列表
     *
     * @param entityName
     * @param applicationId
     * @return
     */
    @Query(value = "select e from EntityView e where e.applicationId = :applicationId and e.entityName = :entityName and e.deleted = false")
    Collection<EntityView> findByEntityName(String entityName, long applicationId);

    /**
     * 通过应用编号获取应用实体列表
     *
     * @param applicationId
     * @return
     */
    @Query(value = "select e from EntityView e where e.applicationId = :applicationId and e.deleted = false")
    Collection<EntityView> findByApplication(long applicationId);

    /**
     * 分页获取应用实体列表
     *
     * @param spec
     * @param pageable
     * @return
     */
    Page<EntityView> findAll(Specification<EntityView> spec, Pageable pageable);
}